<?php
/**
 * User: i@fedax.cn
 * Time: 2018/7/27 17:45
 */

namespace app\admin\model;
use think\cache\driver\Redis;
/**
 * 宝箱奖品
 * Class GoldEggPrize
 * @package app\admin\model
 */
class GoldEggsPrize extends DataBase
{
    protected $name = 'gold_eggs_prize';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'add_time';
    public function getUpdateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * @param $page_config
     * @param $where
     * @param $order
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getDataList()
    {
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );
        $order = input('post.sort', 'ss_gold_eggs_prize.id') . " " . input('post.order', 'desc');
        $where = array();//组装搜索语句

        return $this
            ->view('ss_gold_eggs_prize', 'id,status,prize_stock,default_stock,gift_notice,gold_egg_id,update_time')
            ->view('ss_gift_url', 'gift_name,diamonds', 'ss_gift_url.id = ss_gold_eggs_prize.gift_id')
            ->view('ss_gold_egg', 'egg_name', 'ss_gold_egg.id = ss_gold_eggs_prize.gold_egg_id')
            ->where($where)
            ->order($order)
            ->paginate($page_config);
    }

    public function goldEggsPrizeAdd()
    {
        $redis = new Redis();
        $redis->rm('prizeDisplay');//清除宝箱公示
        $newData = input('post.');
        $newData['prize_stock'] = 0;
        $res = $this->save($newData);
        return $res;
    }

    public function goldEggsPrizeEdit()
    {
        $redis = new Redis();
        $newData = input('get.');
        $res = $this->save($newData,['id'=>$newData['id']]);

        if(!$res)  return ['code'=>-100,'msg'=>'修改失败'];

        // $prize_stock = $this->where('id','=',$newData['id'])->value('prize_stock'); //获取剩余库存

        // halt($prize_stock.'--'.$newData['prize_stock']);

        //  if($prize_stock != $newData['prize_stock']){   //若修改剩余库存则修改队列


        $redis->rm('store:gold_egg_id:'.$newData['gold_egg_id']);//清除该宝箱队列
        $totalStore = $this
            ->where('gold_egg_id','=',$newData['gold_egg_id'])
            ->where('status','=', 1)
            ->sum('prize_stock');

        for ($i = 0; $i < $totalStore; $i++) {
            $len = $redis->lPush('store:gold_egg_id:'.$newData['gold_egg_id'], 1);  //重新队列
        }

        if($len != $totalStore) return ['code'=>-100,'msg'=>'入库成功，但队列长度可能未与剩余库存总数保持一致，可能处在抽奖高峰期'];
        // }

        return ['code'=>100,'msg'=>'修改成功'];
    }


    public function changePrizeStatus()
    {
        $redis = new Redis();
        $id = input('get.id');
        $detail = $this->field('status,gold_egg_id')->where('id','=',$id)->find();

        if(!$detail) return ['code'=>-100,'msg'=>'没找到该礼物'];

        $detail['status'] = $detail['status'] == 0? 1 : 0;
        $res = $this->save(['status'=>$detail['status']],['id'=>$id]);

        if(!$res) return ['code'=>-100,'msg'=>'修改失败'];

        $redis->rm('store:gold_egg_id:'.$detail['gold_egg_id']);//清除该宝箱队列
        $redis->rm('prizeDisplay');//清除宝箱公示
        $totalStore = $this
            ->where('gold_egg_id','=',$detail['gold_egg_id'])
            ->where('status','=', 1)
            ->sum('prize_stock');

        for ($i = 0; $i < $totalStore; $i++) {
            $len = $redis->lPush('store:gold_egg_id:'.$detail['gold_egg_id'], 1);  //重新队列
        }

        if($len != $totalStore) return ['code'=>-100,'msg'=>'入库成功，但队列未能与库存保持一致，可能处在抽奖高峰期'];

        return ['code'=>100,'msg'=>'修改成功'];

    }


    public function getGiftData()
    {
        $data = $this->field('id,gift_id')->select()->toArray();

        foreach($data as $k=>$v){

            $giftInfo =  GiftUrl::where('id','=',$v['gift_id'])->field('gift_name,diamonds')->find();
            $data[$k]['gift_name'] = $giftInfo['gift_name'];
            $data[$k]['diamonds'] = $giftInfo['diamonds'];
        }
        return $data;
    }

}